package com.hairline.service;

import com.github.pagehelper.PageHelper;
import com.hairline.bean.*;
import com.hairline.bean.vo.*;
import com.hairline.mapper.CommentMapper;
import com.hairline.mapper.OrderGoodsMapper;
import com.hairline.mapper.OrderMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Service
public class WxOrderServiceimpl implements WxOrderService{

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderGoodsMapper orderGoodsMapper;
    @Autowired
    CommentMapper commentMapper;


    @Override
    public List<Order> selectOrders(Integer showType, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        OrderExample orderExample = new OrderExample();
        ArrayList<Short> shorts = new ArrayList<>();
        if (showType == 0){
            shorts.add((short) 101);
            shorts.add((short) 102);
            shorts.add((short) 103);
            shorts.add((short) 201);
            shorts.add((short) 202);
            shorts.add((short) 203);
            shorts.add((short) 301);
            shorts.add((short) 401);
            shorts.add((short) 402);
        }else if(showType == 1){
            shorts.add((short) 101);
        }else if (showType == 2){
            shorts.add((short) 201);
        }else if (showType == 3){
            shorts.add((short) 301);
        }else if (showType == 4){
            shorts.add((short) 401);
        }
        orderExample.createCriteria().andOrderStatusIn(shorts);
        List<Order> orders = orderMapper.selectByExample(orderExample);
        return orders;
    }

    @Override
    public List<WxOrderListVo> selectOrderGoods(List<Order> orderList, Integer showType) {
        List<WxOrderListVo> wxOrderListVos = new ArrayList<>();
        //查询数据并给相应的VO数据赋值
        for (int i = 0; i < orderList.size(); i++) {
            WxOrderListVo orderListVo = new WxOrderListVo();
            //获取orderId来查询orderGoods表的商品
            Integer id = orderList.get(i).getId();

            List<WxOrderGoodsVo> orderGoods = orderGoodsMapper.selectByOrderId(id);
            //给VO赋值
            orderListVo.setOrderStatusText(orderList.get(i).getOrderStatus());
            orderListVo.setOrderSn(orderList.get(i).getOrderSn());
            orderListVo.setActualPrice(orderList.get(i).getActualPrice());
            orderListVo.setGoodsList(orderGoods);
            orderListVo.setId(orderList.get(i).getId());



            orderListVo.setHandleOption(new WxOrderHandleOptionVO());
            wxOrderListVos.add(orderListVo);
        }
        return wxOrderListVos;
    }

    @Override
    public int countOrder() {
        return orderMapper.count();
    }

    @Override
    public WxOrderInfolVO selectOrderById(Integer orderId) {
        WxOrderInfolVO orderInfolVO = new WxOrderInfolVO();

        Order order = orderMapper.selectByPrimaryKey(orderId);

        orderInfolVO.setConsignee(order.getConsignee());
        orderInfolVO.setAddress(order.getAddress());
        orderInfolVO.setAddTime(order.getAddTime());
        orderInfolVO.setOrderSn(order.getOrderSn());
        orderInfolVO.setActualPrice(order.getActualPrice());
        orderInfolVO.setMobile(order.getMobile());
        orderInfolVO.setOrderStatusText(order.getOrderStatus());
        orderInfolVO.setGoodsPrice(order.getGoodsPrice());
        orderInfolVO.setCouponPrice(order.getCouponPrice());
        orderInfolVO.setId(orderId);
        orderInfolVO.setFreightPrice(order.getFreightPrice());

        WxOrderHandleOptionVO handleOption = new WxOrderHandleOptionVO();
        if (orderInfolVO.getOrderStatusText().equals("未付款")){
            handleOption.setPay(true).setCancel(true);
        }else if (orderInfolVO.getOrderStatusText().equals("用户取消")){
            handleOption.setDelete(true).setRebuy(true);
        }else if (orderInfolVO.getOrderStatusText().equals("系统取消")){
            handleOption.setDelete(true).setRebuy(true);
        }else if (orderInfolVO.getOrderStatusText().equals("已付款")){
            handleOption.setRefund(true);
        }else if (orderInfolVO.getOrderStatusText().equals("已退款")){
            handleOption.setDelete(true).setRebuy(true);
        }else if (orderInfolVO.getOrderStatusText().equals("已发货")){
            handleOption.setRebuy(true).setRefund(true).setConfirm(true);
        }else if (orderInfolVO.getOrderStatusText().equals("已收货")){
            handleOption.setDelete(true).setRebuy(true).setComment(true);
        }else if (orderInfolVO.getOrderStatusText().equals("系统收货")){
            handleOption.setDelete(true).setRebuy(true).setComment(true);
        }//还有一种情况时该订单正在申请退款，此时没有按钮提供

        orderInfolVO.setHandleOption(handleOption);

        return orderInfolVO;
    }

    @Override
    public List<WxOrderGoodsVo> selectOrderGoodsByOrderId(Integer orderId) {
        List<WxOrderGoodsVo> orderGoods = orderGoodsMapper.selectByOrderId(orderId);
        //根据给的orderStatus来给某些按钮赋值
        return orderGoods;
    }

    @Override
    public boolean updateOrderStatusById(Integer orderId,Integer newOrderSatus) {
        int i = orderMapper.updateOrderStatusById(orderId, newOrderSatus);
        if (i == 1){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteOrderById(Integer orderId) {
        int i = orderMapper.deleteByPrimaryKey(orderId);
        if (i == 1){
            return true;
        }
        return false;
    }

    @Override
    public List<WxOrderGoodsVo> selectOrderGoodsByOrderIdAndGoodsId(Integer orderId, Integer goodsId) {
        List<WxOrderGoodsVo> orderGoods = orderGoodsMapper.selectByOrderIdAndGoodsId(orderId,goodsId);
        return orderGoods;
    }

    @Override
    public boolean commentOrderGoodsById(HashMap<String, Object> map) {
        Integer orderGoodsId = (Integer) map.get("orderGoodsId");
        String content = (String) map.get("content");
        Short start = (Short) map.get("start");
        boolean hasPicture = (boolean) map.get("hasPicture");
        List<String> picUrls = (List<String>) map.get("picUrls");
        //先去orderGoods表查到商品的id，然后再去comment表，创建一行数据
        int goodsId = orderGoodsMapper.selectGoodsId(orderGoodsId);
        Comment comment = new Comment();
        comment.setContent(content);
        comment.setValueId(goodsId);
        comment.setHasPicture(hasPicture);
        comment.setStar(start);
        if (picUrls.size() > 0){
            comment.setPicUrls(picUrls.toArray().toString());
        }
        int i = commentMapper.insertSelective(comment);
        if (i == 1){
            return true;
        }
        return false;
    }
}
